<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetIssuesNumberList.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\duobao;

use app\api\model\dao\Goods;
use app\api\model\dao\IssuesNumber;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;
use app\library\Flag;

class GetIssuesNumberList extends PageBase
{

    private $objIssuesNumber;
    private $objGoods;

    public function __construct()
    {
        parent::__construct();
        $this->objIssuesNumber = new IssuesNumber();
        $this->objGoods = new Goods();
    }

    public function excute($param)
    {

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objIssuesNumber->getCount([]);
        $rets = $this->objIssuesNumber->getIssuesNumberByCond([], [], $offset, $limit);

        if (!empty($rets)) {
            $goodsArr = [];
            $goods_ids = array_map(function ($v) {
                return $v['goods_id'];
            }, $rets);
            $goods = $this->objGoods->getGoodsByCond(['goods_id' => ['in', implode(',', $goods_ids)]]);
            foreach ($goods as $good) {
                $goodsArr[$good['goods_id']] = $good;
            }
            foreach ($rets as &$ret) {
                $ret['goods_name'] = $goodsArr[$ret['goods_id']]['goods_name'];
                $ret['goods_thumb'] = $goodsArr[$ret['goods_id']]['goods_thumb'];
                $ret['goods_brief'] = $goodsArr[$ret['goods_id']]['goods_brief'];
                $ret['goods_img'] = $goodsArr[$ret['goods_id']]['goods_img'];
                $ret['goods_desc'] = $goodsArr[$ret['goods_id']]['goods_desc'];
                $ret['remain'] = $ret['volume'] - $ret['current_volume'];
                $ret['sell_percent'] = '' . (round($ret['current_volume'] / $ret['volume'], 2) * 100) . '%';
                $ret['status_desc'] = Flag::$map_duobao[$ret['status']];
            }
        }

        if ($rets === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        $this->data = array(
            'list' => $rets,
            'total' => $counts,
        );

        return $this->getRes();
    }
}
